const app = getApp();
const log =require('../../log.js');
const util=require('../../util.js');
const tabService = require("../../libs/tabService");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show:false,
        number:0,
        sn:'',
        score:0,
        bindPhone:0,
        times:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that=this;   
        console.log('-----default---options----')
        console.log(options);
        console.log('-----default----options----')
        var parent_id=options.parent_id;
        var store_id=options.id;
        var sn=options.sn;
        if(store_id!=undefined){
            
          console.log('store_id1=>'+store_id)
          this.setData({
            store_id:store_id
          });
          wx.setStorageSync('store_id',store_id);
      }else{
        store_id=options.store_id;
        if(store_id==undefined){
            store_id=wx.getStorageSync('store_id');
        }
        wx.setStorageSync('store_id',store_id);
        console.log('store_id2=>'+store_id)
        this.setData({
              store_id:store_id
            });        
      }
        if(sn!=undefined){
            this.setData({
                sn:sn
            });
            wx.setStorageSync('sn',sn);
        }
        if(parent_id!=undefined){
            this.setData({
                parent_id:parent_id
              })
              wx.setStorageSync('parent_id',parent_id);
        }else{
            parent_id = wx.getStorageSync('parent_id');
        
        }
        this.Index(parent_id,store_id);
        var open=wx.getStorageSync('open'); 
        var opens=wx.getStorageSync('opens'); 
        console.log('open=>'+open);
        var date=util.formatTime(new Date());
        log.info("Time:"+date+",page:default,options:"+JSON.stringify(options));
        log.info("Time:"+date+",page:default,store_id:"+store_id);
        //open="";
        if(open==""){
            wx.navigateTo({
              url: '../login/index?parent_id='+parent_id+'&store_id='+store_id,
            })
        }
        wx.showShareMenu({
            menus: ['shareAppMessage', 'shareTimeline'],
            success(res) {
              console.log(res)
            },
            fail(e) {
              console.log(e)
            }
          }) 
        // this.getTimes();     
        that.setData({
            platform:app.globalData.platform,    
            open:open,
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.platUrl
          })
    },
    goBack(){
        wx.navigateBack();
      },

    Index:function(parent_id,store_id){
        let that = this; 
        console.log('parent_id=>'+parent_id+'store_id='+store_id);
        wx.request({
          url: app.globalData.platUrl+'index/Index',
          data:{from:1,store_id:store_id,parent_id:parent_id},
          success:res=>{
             
              var data=res.data;
              console.log('-- getIndex-- data');
              console.log(data);
            that.setData({
             store_name:data.store_name,
             shareTitle:data.shareTitle,
             shareLogo:data.shareLogo,
             shareIndex:data.shareIndex,
             data:res.data.index,
             store:data.store,
             parent:data.parent
            })
          }
      })
    },


 
  
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        var parent_id=wx.getStorageSync('parent_id');
        parent_id=parent_id?parent_id:0;
        //parent_id=1;
        
        //this.getIndex(parent_id);
        let that=this;
        var open=wx.getStorageSync('open');
        var user_id=open.user_id;
        var sn=this.data.sn;
        console.log('show....sn='+sn);
       
        
     
        if(user_id==1){
            //parent_id=1;
        }
        
        if(user_id!=undefined){
            var date=util.formatTime(new Date());
            if(user_id==parent_id){
                 log.info("Time:"+date+",page:index,function:show,user_id="+user_id+',parent_id='+parent_id);
            }else{
                log.info("Time:"+date+",page:index,function:show,user_id="+user_id+',parent_id='+parent_id);
                if(parent_id){
                    that.data.timer = setInterval(
                        function () {
                    that.toClock();        
                    }, 1000);
            
                    that.setData({
                        timer:that.data.timer
                    });
                }
            }
        }
 
 
    
        var role=wx.getStorageSync('role')
        
        this.update(role)    
        tabService.updateIndex(this, 1);
      },
      update(role) {
        tabService.updateRole(this, role);
      },
    getTimes:function(){
        let that=this;
         that.data.timerss = setInterval(
            function () {
        that.toTime();        
        }, 1000);

        that.setData({
            timerss:that.data.timerss
        });
    },
    toTime:function(){
        var times=this.data.times;
        var date = new Date();
        let hour = date.getHours(); 
        let minutes = date.getMinutes();    //分
        let seconds = date.getSeconds();    //秒
        if(hour>17){
            times='本时间段不能抢券，请关注明日抢券时间！';
           
        }else if(hour<7){
            times='本时间段不能抢券，请关注今日抢券时间！';
        }else if(hour==17){
            if(minutes<10){
                times='抢券中';
            }else{
                times='本时间段不能抢券，请关注明日抢券时间！';
            }
        }else if(hour==7){
            times='距离下轮抢券还有'+(60-minutes)+'分'+(60-seconds)+'秒';
        }else{
        if(minutes<10){
            times='抢券中';
        }else{
            times='距离下轮抢券还有'+(60-minutes)+'分'+(60-seconds)+'秒';
        }
        } 
        console.log(times);
        this.setData({
            times:times
        })
    },
    toClock:function(){
        var number=this.data.number;
        var sn=wx.getStorageSync('sn');
        var score=wx.getStorageSync('score');
        if(score==1){
            var time2=new Date();
            var t2=parseInt(time2.getTime()/1000);
            var t1=wx.getStorageSync('scoreTime');
            console.log('t1='+t1);
            console.log('t2='+t2);
            if(t2>t1){
                score=0;
                console.log('t2<t1');
                wx.setStorageSync('score',0);
            }else{
                console.log('t2<t1');
            }
        }else{
            console.log('score==0');
        }
        if(number>=2){
            clearInterval(this.data.timer);
            console.log(number);
            this.setData({
            timer: null
            });
            let that = this;
            var parent_id = wx.getStorageSync('parent_id');
            parent_id = parent_id?parent_id:0;
           // parent_id=1;
            var open = wx.getStorageSync('open');
            var user_id = open.user_id;
            //var user_id=5677;
            var role = open.role; 
            if(score==1){
                console.log('score is already');
            }else{
                console.log('setshare');
                wx.request({
                    url: app.globalData.platUrl+'index/setShare',
                    data:{from:1,parent_id:parent_id,user_id:user_id,role:role,sn:sn},
                    success:res=>{
                        var data = res.data;
                      console.log(data);
                    }
                })
                wx.setStorageSync('score',1);
                var time1=new Date();
                time1.setDate(1);
                time1.setHours(0);
                time1.setMinutes(0);
                time1.setSeconds(0);
                time1.setMonth(time1.getDay()+1);        
                var t1=parseInt(time1.getTime()/1000);
                wx.setStorageSync('scoreTime',t1);
            } 
         
        }else{
            number = number+1;
            console.log(number);
            this.setData({
                number:number
            })
        }
    },
    closeMask:function(){
        this.setData({
            show:false
        })
    },
    goIndex(){
        wx.setStorageSync('store_id',0);
        wx.reLaunch({
          url: 'index',
        })
    },
    goto:function(e){
        console.log(e);
        var url=e.currentTarget.dataset.url;
        var parent_id=wx.getStorageSync('parent_id');
        var index=e.currentTarget.dataset.index;
        if(url=="#"){

        }else if(url=='phone'){
            wx.makePhoneCall({
              phoneNumber: this.data.store.phone,
            })
        }else if(url=='coupon'){
            var date=util.formatTime(new Date());
            var face_value=e.currentTarget.dataset.face_value;
            var pay_money=e.currentTarget.dataset.pay_money;
            var yxq=e.currentTarget.dataset.yxq;
            var month=e.currentTarget.dataset.month;
             log.info("Time:"+date+",page:index,function:goto:,index"+index);
            this.getCoupon(face_value,month,yxq,pay_money);
        }else{
            if(index=='index'){
                wx.reLaunch({
                    url: url,
                  })
            }else{
                console.log(url);
                wx.navigateTo({
                    url: url,
                  })
            }
       
        }
      // console.log(url);
    },
    getCoupon:function(face_value,month,yxq,pay_money){
        console.log('index='+face_value);
        var date=util.formatTime(new Date());
        log.info("Time:"+date+",page:index,function:getCoupon,face_value="+face_value);
        let that = this;   
        var open=wx.getStorageSync('open'); 
        var user_id= open.user_id;
        var parent_id=wx.getStorageSync('parent_id');
        if(parent_id==undefined){
            parent_id=0;
        }
        var modelIndex=this.data.modelsIndex;
        var phone=open.phone;
        var couponStatus=this.data.couponStatus;
        var store_id=wx.getStorageSync('store_id');
        console.log(phone+'phone');
        console.log(store_id+'phone');
        // phone='';
        if(phone){
            var date = new Date();
            var hour = date.getHours();
            //alert(hour);
            //hour=7;
            console.log(hour)
            if(hour<4){
                that.showModels('该时间段不能抢','#','#');
            }else if(hour>17){
                that.showModels('该时间段不能抢','#','#');
            }else{
                wx.request({
                    url: app.globalData.platUrl+'coupon/getCoupon',
                    data:{from:1,store_id:store_id,parent_id:parent_id,user_id:user_id,face_value:face_value,month:month,yxq:yxq,pay_money:pay_money,couponStatus:1,models:0},
                    success:res=>{
                        console.log(res.data);
                        var date=util.formatTime(new Date());
                        var data=res.data;
                        log.info("Time:"+date+",page:index,function:getCoupon,user_id="+JSON.stringify(data));
                        that.showModels(data.msg,data.url,data.index);
                    }
                })
            }
        }else{
         that.setData({
             bindPhone:1
         })
        }
      
    },
    showModels:function(msg,url,index){
        wx.showModal({
          cancelColor: 'cancelColor',
          title:'系统提示',
          content:msg,
          //showCancel:false,
          success:function(e){
           
                if(url=='#'){

                }else{
                    if(index=="#"){
                        wx.navigateTo({
                          url: url,
                        })
                   }
                   if(index=="index"){
                    wx.reLaunch({
                      url: url,
                    })
               }
                }          
                         
          }
          

        })

    },
    cancel(){
        this.setData({
            bindPhone:0
        })
    },
    getPhoneNumber (e){
        let that=this;
        var open=wx.getStorageSync('open');
        console.log(e);
       
                wx.request({
                    url:app.globalData.platUrl+'login/getPhoneNumber',
                    data:{
                      code:e.detail.code,
                       id:open.user_id,
                       type:3
                    },
                    method:'GET',
                    header:{
                        'content-type':'application/json'
                    },
                    success:function(res){
                      console.log(res)
                        //wx.setStorageSync('PhoneNumber',res.data.phone);
                        if(res.data.code==200){
                          open.phone = res.data.phone;
                          wx.setStorageSync('open', open);
                          that.setData({
                              open:open,
                              bindPhone:0,
                              phone:res.data.phone
                          })
                          that.showModels('获取手机号成功',"#");
                        }else{
                            that.showModels('获取手机号失败','#');
                        } 
                        //that.show();
                        //console.log("手机号为");
                        //console.log(res);
                    },
                    fail:function(err){
                        console.log(err);
                    }
                })
       
      },
    goShopcart:function(){
        wx.navigateTo({
          url: '../shopcart/index',
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        clearInterval(this.data.timer);
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        clearInterval(this.data.timer);
        clearInterval(this.data.timerss);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },
    setShare:function(id,page,md5){
        var open=wx.getStorageSync('open');
        var date=new Date();
        var time=date.getTime();
        var user_id=open.user_id;
        
        wx.request({
            url: app.globalData.platUrl+'share/index',
            data:{user_id:user_id,from:3,id:id,page:page,sn:md5},
            success:function(res){
                console.log(res);
            }
        }) 
   
    
        console.log('setShare');
        //var md5=;

    },
    //分享朋友圈
    onShareTimeline(){
        //console.log(e);
        var title=this.data.shareTitle;
        var shareLogo=this.data.shareLogo;
        var open=wx.getStorageSync('open'); 
        var store_id=wx.getStorageSync('store_id');       
        var parent_id=open.user_id;
        var date=new Date();
        var time=date.getTime();
        var page='Index';
        var time=date.getTime();
        var second=date.getSeconds();
        var hour=date.getHours();
        var day=date.getDate();
    
        var md5=day+'I'+hour+'nd'+second+'ex';
         this.setData({
           share:true
         });
         console.log(store_id);
         console.log('onShareTimeLine');
         console.log(shareLogo);
         console.log(title);
         var date=new Date();
         var time=date.getTime();
         this.setShare(1,'index',md5);
         return{
           title: title,                 
           query: 'parent_id='+parent_id+'&sn='+md5+'&store_id='+store_id,        
           imageUrl:shareLogo      
           }
       },
    /**
     * 用户点击右上角分享
     */
    /** 分享好友* */
    onShareAppMessage(e) {
        var open=wx.getStorageSync('open');
        var parent_id=open.user_id;
        var store_id=wx.getStorageSync('store_id');
        var that = this;
        var title=this.data.shareTitle;
        var shareLogo=this.data.shareIndex;
        var date=new Date();
        var time=date.getTime();
        var page='Index';
        var time=date.getTime();
        var second=date.getSeconds();
        var hour=date.getHours();    
        var day=date.getDate();    
        var md5=day+'I'+hour+'nd'+second+'ex';
        console.log(store_id);
   　　var shareObj = {
   　　　　title: title,    
   　　　　path: '/pages/index/index?parent_id='+parent_id+'&sn='+md5+'&store_id='+store_id,    
   　　　　imageUrl: shareLogo 
   　　　
   　　}
   　　// 来自页面内的按钮的转发
   　　if( e.from == 'button' ){
     　　　shareObj.title=title;
   　　　　shareObj.path = '/pages/index/index?parent_id='+parent_id+'&sn='+md5+'&store_id='+store_id;
   　　}
   console.log(shareObj);
   console.log('onShareApp');
  
   this.setShare(2,'index',md5);
  
   　　// 返回shareObj
   　　return shareObj;
    }
})